<?php namespace app\common\service;
use  \app\common\model\Area as AreaModel;
use think\Exception;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/19
 * Time: 15:22
 */
class Area extends Base{
    public function __construct()
    {
        parent::__construct();
        $AreaModel = new AreaModel();
        $this->model = $AreaModel;
    }

    /**
     *  获取某个城市下
     */
    public static function getAreaChild($id){
        $map = ['pid'=>$id];
       return \app\common\model\Area::where($map)->field('id,name,level,price')->select();
    }

    /**
     * 查询直辖市是否有过代理
     * @param $id 城市id(省或者市id)
     * @param $user_id 当前用户id
     * @return bool 返真假
     */
    public function getAreaDirectly($id,$user_id){
        try{

            $status=false;
            $Directly=array(1=>'北京',2=>'北京',19=>'天津',20=>'天津',801=>'上海',802=>'上海',2323=>'重庆',2324=>'重庆');
            $user =new \app\common\model\User();
            if(!empty($Directly[$id])){
                $shortname=$Directly[$id];
                $shortIds=$this->model
                    ->where(['shortname'=>array('eq',$shortname)])
                    ->order('id asc')
                    ->column('id');
                $is_status=$user->where(['province'=>array('eq',$shortIds[0]),'server_type'=>array('eq',1)])->count();
                if($is_status>0){
                    $status=true;
                }else{
                    $provinceproxy_count=Db::name('proxylog')->where(['province'=>$shortIds[0],'server_type'=>array('eq',1),'status'=>array('eq',1)])->count();
                    if($provinceproxy_count>0){
                        $status=true;
                    }else{
                        $is_status=$user->where(['city'=>array('eq',$shortIds[1]),'server_type'=>array('eq',2)])->count();
                        if($is_status>0){

                        }else{
                            $cityproxy_count=Db::name('proxylog')->where(['city'=>$shortIds[1],'server_type'=>array('eq',2),'status'=>array('eq',1)])->count();
                            if($cityproxy_count>0){
                                $status=true;
                            }else{
                                $status=false;
                            }
                        }
                    }

                }

            }
        }catch (Exception $e){
            $status=false;
        }
        return $status;
    }

}